﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SZSLSystem.Common;

namespace SZSLSystem.Web.Admin.Manage
{
    public partial class Add : SZSLSystem.Web.UI.ManagePage
    {
        #region 全局变量


        #endregion 

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RoleBind(this.chkRole);
                OrganizationBind(this.chkOrganization);
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SZSLSystem.Model.Administrator model = new SZSLSystem.Model.Administrator();
                SZSLSystem.BLL.Administrator bll = new SZSLSystem.BLL.Administrator();
                string userLevel = string.Empty;
                if (bll.Exists(this.txtUserName.Text.Trim()))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误了！</b>用户名已存在，请输入别的用户帐号吧！", "", "Error");
                    return;
                }
                model.ID = Guid.NewGuid();
                model.UserName = this.txtUserName.Text.Trim();
                model.UserPassword = this.txtUserPwd.Text.Trim();
                model.Salt = StringPlus.GetRandomNumberString(10, false);
                model.RealName = this.txtReadName.Text.Trim();
                model.Email = this.txtUserEmail.Text.Trim();
                model.Mobile = this.txtMobile.Text.Trim();
                model.Telphone = this.txtTelphone.Text.Trim();
                model.QQ = this.txtQQ.Text.Trim();
                model.Status = Convert.ToInt32(rblIsLock.SelectedValue);
                model.AddDate = DateTime.Now;
                bll.Add(model);

                SZSLSystem.BLL.OrganizationUser orgbll = new BLL.OrganizationUser();
                for (int i = 0; i < this.chkOrganization.Items.Count; i++)
                {
                    if (this.chkRole.Items[i].Selected)
                    {
                        SZSLSystem.Model.OrganizationUser orgmodel = new Model.OrganizationUser();
                        orgmodel.ID = Guid.NewGuid();
                        orgmodel.UserID = model.ID;
                        orgmodel.OrganizationID = Guid.Parse(this.chkOrganization.Items[i].Value.ToString());
                        orgbll.Add(orgmodel);
                    }
                }

                SZSLSystem.BLL.UserRole userbll = new BLL.UserRole();
                for (int i = 0; i < this.chkRole.Items.Count; i++)
                {
                    if (this.chkRole.Items[i].Selected)
                    {
                        SZSLSystem.Model.UserRole usermodel = new Model.UserRole();
                        usermodel.ID = Guid.NewGuid();
                        usermodel.UserID = model.ID;
                        usermodel.RoleID = Guid.Parse(this.chkRole.Items[i].Value.ToString());
                        userbll.Add(usermodel);
                    }
                }
                //
                //日志写入
                //
                SaveLogs("添加用户信息成功！", 0);

                JscriptPrint("添加用户信息成功啦！", "List.aspx", "Success");
            }
            catch (Exception err)
            {
                //
                //异常日志写入
                //
                SaveLogs(err.ToString(), 1);

                JscriptMsg(350, 230, "错误提示", "<b>出现错误了！</b>添加用户信息失败啦！", "", "Error");
            }
        }
    }
}
